package entity;

import client.Client;
import java.util.LinkedList;
import java.util.Date;

public class Subscription implements Cloneable
{
	private static int next_sub_num=1;	//used to know which sub num is taken and which can be used
	private String subscription_number;
	private String customer_id;
	private String car_number;
	private Date start_date;
	private Date end_date;
	public enum subOrderEnum {CORRECT,ID_ERROR,CAR_NUMBER_ERROR};
	public Subscription()
	{
		this.start_date=new Date();
		this.end_date=new Date();
		this.end_date.setTime(this.end_date.getTime()+1000*60*60*24*28);
		this.subscription_number=String.valueOf(Subscription.next_sub_num);
		Subscription.next_sub_num++;
		}
	public String getSubscription_number() {
		return subscription_number;
	}
	public void setSubscription_number(String subscription_number) {
		this.subscription_number = subscription_number;
	}
	public Date getStart_date() {
		return start_date;
	}
	public void setStart_date(Date start_date) {
		this.start_date = start_date;
	}
	public String getCar_number() {
		return car_number;
	}
	public void setCar_number(String car_number) {
		this.car_number = car_number;
	}
	public String getCustomer_id() {
		return customer_id;
	}
	public void setCustomer_id(String customer_id) {
		this.customer_id = customer_id;
	}
	public Subscription clone()
	{
		Subscription sub=new Subscription();
		Subscription.next_sub_num--;
		sub.subscription_number=this.subscription_number;
		sub.customer_id=this.customer_id;
		sub.car_number=this.car_number;
		sub.start_date=this.start_date;
		return sub;
	}
	
	public subOrderEnum validate()
	{
		if (this.customer_id.length() != 9)
			return subOrderEnum.ID_ERROR;
		try{
			  Integer.parseInt(this.customer_id);
			  // is an integer!
			} catch (NumberFormatException e) {
				return subOrderEnum.ID_ERROR;// not an integer!
			}
		
		if (this.car_number.length() != 7)
			return subOrderEnum.CAR_NUMBER_ERROR;
		try{
			  Integer.parseInt(this.car_number);
			  // is an integer!
			} catch (NumberFormatException e) {
				return subOrderEnum.CAR_NUMBER_ERROR;// not an integer!
			}
		
		return subOrderEnum.CORRECT;	
	}
	
	
	public void save()
	{
		Client.saveSubscriptionToDB((Object)this);
	}
	
	public String toString ()
	{
		return("costumer id:  "+this.getCustomer_id()+"\ncar number:  "+this.getCar_number()+"\nsubscription number:  "+this.getSubscription_number()+"\ndate:"+this.getStart_date().toString());
	}
	public Date getEnd_date() {
		return end_date;
	}
	public void setEnd_date(Date end_date) {
		this.end_date = end_date;
	}
	
}
